<?php

declare (strict_types=1);

namespace magein\think\pay\model;

use magein\pay\PayConfig;
use magein\think\utils\MainModel;

/**
 * @property string $pay_no
 * @property string $order_no
 * @property string $channel_id
 * @property string $platform_id
 * @property integer $scene
 * @property string $user_id
 * @property string $subject
 * @property string $appid
 * @property string $merchant_id
 * @property string $trade_no
 * @property string $trade_type
 * @property integer $total_amount
 * @property integer $coupon_amount
 * @property string $coupon_info
 * @property integer $result
 * @property string $reason
 * @property string $remark
 * @property string $notify_date
 * @property string $notify_data
 * @property string $notify_url
 * @property string $return_url
 * @method static PayOrderModel|null _payNo($pay_no);
 * @method static \think\paginator\driver\Bootstrap|null __payNo($pay_no);
 * @method static \think\model\Collection|null ___payNo($pay_no);
 * @method static PayOrderModel|null _orderNo($order_no);
 * @method static \think\paginator\driver\Bootstrap|null __orderNo($order_no);
 * @method static \think\model\Collection|null ___orderNo($order_no);
 * @method static PayOrderModel|null _channelId($channel_id);
 * @method static \think\paginator\driver\Bootstrap|null __channelId($channel_id);
 * @method static \think\model\Collection|null ___channelId($channel_id);
 * @method static PayOrderModel|null _platformId($platform_id);
 * @method static \think\paginator\driver\Bootstrap|null __platformId($platform_id);
 * @method static \think\model\Collection|null ___platformId($platform_id);
 * @method static \think\paginator\driver\Bootstrap|null __scene($scene);
 * @method static \think\model\Collection|null ___scene($scene);
 * @method static PayOrderModel|null _userId($user_id);
 * @method static \think\paginator\driver\Bootstrap|null __userId($user_id);
 * @method static \think\model\Collection|null ___userId($user_id);
 * @method static PayOrderModel|null _merchantId($merchant_id);
 * @method static \think\paginator\driver\Bootstrap|null __merchantId($merchant_id);
 * @method static \think\model\Collection|null ___merchantId($merchant_id);
 * @method static PayOrderModel|null _tradeNo($trade_no);
 * @method static \think\paginator\driver\Bootstrap|null __tradeNo($trade_no);
 * @method static \think\model\Collection|null ___tradeNo($trade_no);
 * @method static \think\paginator\driver\Bootstrap|null __result($result);
 * @method static \think\model\Collection|null ___result($result);
 */
class PayOrderModel extends MainModel
{

    protected $table = 'pay_orders';

    /**
     * 交易结果:等待中
     */
    const RESULT_PENDING = 0;

    /**
     * 交易结果:成功
     */
    const RESULT_SUCCESS = 1;

    /**
     * 交易结果:失败
     */
    const RESULT_FAIL = -1;


    /**
     * @param string|int|null $value
     * @return string|array
     */
    public static function translateScene($value = null)
    {
        $scenes = PayConfig::scenes() ?: [];
        $data = [];
        foreach ($scenes as $item) {
            $data[$item['value']] = $item['label'];
        }

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }

    /**
     * @param $value
     * @return int
     */
    public function setSceneAttr($value): int
    {
        return intval($value);
    }

    /**
     * @param $value
     * @param $data
     * @return string
     */
    public function getSceneTextAttr($value, $data)
    {
        $value = $data['scene'] ?? '';
        if (empty($value)) {
            return '';
        }

        return self::translateScene($value);
    }


    /**
     * @param string|int|null $value
     * @return string|array
     */
    public static function translateResult($value = null)
    {
        $data = [
            self::RESULT_PENDING => '等待中',
            self::RESULT_SUCCESS => '成功',
            self::RESULT_FAIL => '失败'
        ];

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }

    /**
     * @param $value
     * @return int
     */
    public function setResultAttr($value): int
    {
        return intval($value);
    }

    /**
     * @param $value
     * @param $data
     * @return string
     */
    public function getResultTextAttr($value, $data)
    {
        $value = $data['result'] ?? '';
        if (empty($value)) {
            return '';
        }

        return self::translateResult($value);
    }
}